package com.superdry.adapter;

import java.util.List;

import com.ab.http.AbHttpUtil;
import com.ab.http.AbRequestParams;
import com.ab.http.AbStringHttpResponseListener;
import com.drysurper.R;
import com.superdry.activity.ChooseCityActivity;
import com.superdry.fragment.Fragment_Dry;
import com.superdry.json.Location;
import com.superdry.utils.GlobalConst;
import com.superdry.utils.ToastHelper;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class CityListAdapter extends BaseAdapter {

	private ChooseCityActivity context;
	private List<Location> locations;
	private AbHttpUtil mAbHttpUtil;
	public CityListAdapter(ChooseCityActivity context, List<Location> locations) {
		this.context = context;
		this.locations = locations;
		// 获取Http工具类
		mAbHttpUtil = AbHttpUtil.getInstance(context);
		mAbHttpUtil.setTimeout(10000);
	}

	@Override
	public int getCount() {
		return locations.size();
	}

	@Override
	public Location getItem(int position) {
		return locations.get(position);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}	

	@Override
	public View getView(final int position, View view, ViewGroup parent) {
		ViewHolder viewHolder;
		final Location location = getItem(position);
		if (view == null) {
			viewHolder = new ViewHolder();
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inflater.inflate(R.layout.city_item, parent, false);
			viewHolder.city_name = (TextView) view.findViewById(R.id.city);
			viewHolder.area = (TextView) view.findViewById(R.id.area);
			viewHolder.divider = view.findViewById(R.id.divider);
			view.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) view.getTag();
		}

		if (position == 0 || !getItem(position).getLocationProvince().equals(getItem(position - 1).getLocationProvince())) {
			viewHolder.city_name.setVisibility(View.VISIBLE);
			viewHolder.divider.setVisibility(View.VISIBLE);
			viewHolder.city_name.setText(location.getLocationProvince());
		} else {
			viewHolder.divider.setVisibility(View.GONE);
			viewHolder.city_name.setVisibility(View.GONE);
		}
		viewHolder.area.setText(location.getLocationProvince()+"/"+location.getLocationArea());
		viewHolder.area.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View view) {
				
				AbRequestParams params = new AbRequestParams();
				params.put("locationId", location.getLocationId()+"");
				mAbHttpUtil.post(GlobalConst.GET_SELECTED_LOCATION, params, new AbStringHttpResponseListener() {
					
					@Override
					public void onStart() {
						
					}
					
					@Override
					public void onFinish() {
						
					}
					
					@Override
					public void onFailure(int statusCode, String content, Throwable error) {
						ToastHelper.show(context, "选择失败");
					}
					
					@Override
					public void onSuccess(int statusCode, String content) {
						Intent intent = new Intent();
						intent.putExtra("city_name", getItem(position).getLocationProvince()+"/"+getItem(position).getLocationArea());
						context.setResult(Fragment_Dry.CHOOSE_CITY, intent);
						context.finish();
					}
				});
			}
		});
		return view;
	}

	class ViewHolder {
		TextView city_name;
		TextView area;
		View divider;
	}
}
